<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2020/1/7 16:01
// +----------------------------------------------------------------------

namespace app\single\api;


use think\Db;
use think\facade\Log;

class PlayerKeep
{
    public function run($params)
    {
        $data["ret"] = 1;
        $data["data"] = [];
        $sDate = date("Ymd", strtotime($params["sDate"]));
        $statDate = $sDate;
        //if ($params["debug"] == 0) {
        //    $sDate = date("Ymd", strtotime("-31 days"));
        //}
        $eDate = date("Ymd", strtotime($params["eDate"]));
        $keepData = Db::table("c_player_keep")->whereBetween("day", [$sDate, $eDate])->select();
        $sql = Db::table("c_player_keep")->getLastSql();
        Log::write("player keep data0000" . json_encode($keepData)."sql===<>".$sql."params===>".json_encode($params));
        if ($keepData) {
            foreach ($keepData as $row) {
                $data["data"][$row["day"] . "_" . $row["type"] . "_" . $row["platform"]][$row["keep_day"]] = $row["player_num"];
            }
        }
        Log::write("player keep data11111" . json_encode($data));
        //注册用户数
        $regData = Db::table("c_player_online")->alias("po")
//            ->join([config("database.game").".role" => "r"], "r.id = po.player_id")
            ->whereBetween("day", [$statDate, $eDate])
            ->where("is_new", 1)
            ->field("day,COUNT(player_id) AS num,po.platform")
            ->group("day,po.platform")
            ->select();
        if ($regData) {
            foreach ($regData as $row) {
                $data["data"][$row["day"] . "_1_" . $row["platform"]][0] = $row["num"];
            }
        }
        //付费用户数
        $sql="SELECT
  first_pay_date AS `day`,
  platform,
  COUNT(role_id) AS new_pay_users
FROM (
  SELECT
    role_id,
    platform,
    MIN(pay_date) AS first_pay_date
  FROM role_pay_log
  GROUP BY role_id
) t
WHERE first_pay_date BETWEEN $statDate AND $eDate  -- 指定日期范围
GROUP BY first_pay_date, platform
ORDER BY first_pay_date, platform;";
// Log::write("debug count pay user".$sql);
    $payData = Db::query($sql);
        // $payData = Db::table("c_player_online")
        //     ->whereBetween("day", [$statDate, $eDate])
        //     ->where("is_pay", 1)
        //     // ->where("contin_pay", 1)
        //      ->where("player_id NOT IN (SELECT player_id FROM  c_player_online  WHERE day != '".$statDate."' AND is_pay = 1 group by player_id)")
        //     ->field("day,COUNT(player_id) AS num,platform")
        //     ->group("day,platform")
        //     ->select();
        if ($payData) {
            foreach ($payData as $row) {
                $data["data"][$row["day"] . "_2_" . $row["platform"]][0] = $row["new_pay_users"];
            }
        }

        Log::write("player keep data" . json_encode($data));
        return $data;
    }
}
